package com.example.web.controller;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.example.web.entity.User;
import com.example.web.service.UserService;
import com.example.web.tool.R;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
public class User1Controller {

	@Autowired
	UserService userService;

	/**
	 * --------------------------------	增 -----------------------------
	 * @param id
	 * @return
	 */
	@PostMapping("/insert")
	public R save1(User user) {
		log.info("save1 user:{}", user);
		// 选择部分字段插入
		return R.ok().put("data", userService.insert(user));
	}

	@PostMapping("/insertAllColumn")
	public R save2(User user) {
		log.info("save2 user:{}", user);
		// 全字段插入
		return R.ok().put("data", userService.insertAllColumn(user));
	}

	@PostMapping("/insertBatch")
	public R save3(@RequestBody User[] users) {
		log.info("save3 users:{}", Arrays.toString(users));
		List<User> list = Arrays.asList(users);
		// 批量插入
		return R.ok().put("data", userService.insertBatch(list));
	}

	@PostMapping("/insertOrUpdate")
	public R save4(User user) {
		log.info("save4 user:{}", user);
		//主键重复就修改，不重复存
		return R.ok().put("data", userService.insertOrUpdate(user));
	}
	
	/**
	 * 	-----------------------------------------删-------------------------------
	 * @return
	 */
	@DeleteMapping("/deleteById")
	public R remove1(Integer id) {
		log.info("remove1 id:{}",id);
		//删除
		return R.ok().put("data", userService.deleteById(id));
	}
	@DeleteMapping("/delete")
	public R remove2(Integer id) {
		log.info("remove2 id:{}",id);
		//这个延展性很强
		return R.ok().put("data", userService.delete(new EntityWrapper<User>().eq("id", id)));
	}
	@DeleteMapping("/deleteBatchIds")
	public R remove3(@RequestBody Integer[] ids) {
		log.info("remove3 ids:{}", Arrays.toString(ids));
		List<Integer> list = Arrays.asList(ids);
		//批量删除
		return R.ok().put("data", userService.deleteBatchIds(list));
	}
	/**
	 * 	-------------------------------------------改--------------------------------------
	 */
	@PutMapping("/updateById")
	public R change1(User user) {
		log.info("change1 user:{}", user);
		return R.ok().put("data", userService.updateById(user));
	}
	@PutMapping("/update")
	public R change2(User user) {
		log.info("change2 user:{}", user);
		return R.ok().put("data", userService.update(user, new EntityWrapper<User>().eq("name","keke")));
	}
	@PutMapping("/insertOrUpdate")
	public R change3(User user) {
		log.info("change3 user:{}", user);
		return R.ok().put("data", userService.insertOrUpdate(user));
	}
	/**
	 * 	-------------------------------------------------查-----------------------------------------
	 */
	@GetMapping("/selectById{id}")
	public R get1(@PathVariable Integer id) {
		log.info("get1 id:{}",id);
		return R.ok().put("data", userService.selectById(id));
	}
	@GetMapping("/selectObj{id}")
	public R get2(@PathVariable Integer id) {
		log.info("get2 id:{}", id);
		return R.ok().put("data", userService.selectObj(new EntityWrapper<User>().eq("id", id)));
	}
	@GetMapping("/selectCount")
	public R get3() {
		log.info("get3");
		return R.ok().put("data", userService.selectCount(new EntityWrapper<User>()));
	}
	@GetMapping("/selectList")
	public R get4() {
		log.info("get4");
		return R.ok().put("data", userService.selectList(new EntityWrapper<User>()));
	}
	@GetMapping("/selectPage")
	public R get5() {
		log.info("get5");
		return R.ok().put("data", userService.selectPage(new Page<User>(1,10)));
	}
}
